package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Class;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.ClassService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@CrossOrigin
public class ClassController {

    @Autowired
    private ClassService classService;

    /**
     * 添加班级
     * @param cls
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/addClassRoom",method = RequestMethod.POST)
    public Result addClass(Class cls){
        System.out.println("添加班级");
        try {
            classService.addClass(cls);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 删除班级
     * @param classId
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/deleteClass")
    public Result deleteClass(Integer classId){

        try {
            classService.deleteClass(classId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 更新班级
     * @param cls
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/updateClassRoom",method = RequestMethod.POST)
    public Result updateClass(Class cls){

        try {
            classService.updateClass(cls);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 通过班级ID查询班级信息
     * @param classId
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/getClassRoomByClassId")
    public Result getClassById(Integer classId){

        try {
            Class classById = classService.getClassById(classId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(classById);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/getClassRoomPage")
    public Result getClassPage(Integer pageNo,Integer pageSize){

        try {
            Page classPage = classService.getClassPage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(classPage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }
}
